***** RUN DO_MASTER.do ***** do "$Do_files\DO_MASTER.do"

global program "Main_Figure1"
cap log close
cap log using "$Log_files\log_$program$today", replace

**** REPLICATION OF FIGURE 1 ****

clear all
set mem 2g
set more off

// Import data

use $Data\data_crime_news, clear

// Individual controls vector

global i_controls "age age2 i.sex_cd_p relation_* i.recidive_p pk_article_cd111 pk_article_cd112 pk_article_cd113 pk_article_cd116 pk_article_cd117 pk_article_cd137 pk_article_cd187" //

// Regression

reghdfe coverage_nat ///
	election_547_M911_etr election_547_M68_etr election_547_M35_etr election_547_M02_etr ///
	election_547_F02_etr election_547_F35_etr election_547_F68_etr election_547_F911_etr ///
	Ddist_min readershare ///
	$i_controls if pk_date<=18596, absorb(pk_wy pk_dow pk_commune newspaper) cluster(pk_case)

// Labels

cap la var election_547_M02_etr 	`" "Sep 1, 2009"   "Nov 29, 2009" "'
cap la var election_547_M35_etr 	`" "Jun 1, 2009"   "Aug 31, 2009" "'
cap la var election_547_M68_etr 	`" "Mar 1, 2009"   "May 31, 2009" "'
cap la var election_547_M911_etr 	`" "Jan 1, 2009"   "Feb 28, 2009" "'
cap la var election_547_M_etr 		`" "Jan 1, 2009"   "Nov 29, 2009" "'

cap la var election_547_F02_etr 	`" "Nov 30, 2009"   "Feb 28, 2010" "'
cap la var election_547_F35_etr 	`" "Mar 1, 2010"   "May 31, 2010" "'
cap la var election_547_F68_etr 	`" "Jun 1, 2010"   "Aug 31, 2010" "'
cap la var election_547_F911_etr 	`" "Sep 1, 2010"   "Nov 29, 2010" "'
cap la var election_547_F_etr 		`" "Nov 30, 2009"   "Nov 29, 2010" "'

// Plot coefficients

coefplot, keep(election_547_*) level(90) mlabel mlabsize(small) format(%9.3f) mlabposition(9) vertical ciopts(recast(rcap)) ///
	yline(0, lpattern(dash)) xline(4.5, lpattern(dash) lcolor(gs13)) ylabel(-.05 0 "0" .05(.05).2, labsize(small) angle(0) format(%9.2f)) xlabel(, labsize(vsmall)) ///
	ytitle("Foreign perpetrator coefficient (90% C.I.)") xtitle("Before referendum | After referendum   ", margin(medsmall)) ///
	graphregion(color(white)) 
estimates store est2

// Test coefficients

test election_547_M911_etr election_547_M68_etr election_547_M35_etr election_547_M02_etr
test election_547_F02_etr election_547_F35_etr election_547_F68_etr election_547_F911_etr 

matrix b=e(b)
g bM0911=b[1,1]
g bM0608=b[1,2]
g bM0305=b[1,3]
g bM0002=b[1,4]
g bF0002=b[1,5]
g bF0305=b[1,6]
g bF0608=b[1,7]
g bF0911=b[1,8]

matrix V=e(V)
g seM0911=sqrt(V[1,1])
g seM0608=sqrt(V[2,2])
g seM0305=sqrt(V[3,3])
g seM0002=sqrt(V[4,4])
g seF0002=sqrt(V[5,5])
g seF0305=sqrt(V[6,6])
g seF0608=sqrt(V[7,7])
g seF0911=sqrt(V[8,8])

g df_full=e(df_r)
foreach y in M0911 M0608 M0305 M0002 F0002 F0305 F0608 F0911{
	g t`y'=b`y'/se`y'
	g p`y'=2*ttail(df_full, abs(t`y'))
}

// Pre and post votation

reghdfe coverage_nat election_547_M_etr election_547_F_etr Ddist_min readershare ///
	$i_controls if pk_date<=18596, absorb(pk_wy pk_dow pk_commune newspaper) cluster(pk_case) //
estimates store est3

matrix b=e(b)
g bM=b[1,1]
g bF=b[1,2]

matrix V=e(V)
g seM=sqrt(V[1,1])
g seF=sqrt(V[2,2])

g df_group=e(df_r)
foreach y in M F{
	g t`y'=b`y'/se`y'
	g p`y'=2*ttail(df_group, abs(t`y'))
}

keep bM* bF* seM* seF* df_* tM* tF* pM* pF*
duplicates drop

foreach t in M0911 M0608 M0305 M0002 F0002 F0305 F0608 F0911{
	foreach var in b se t p{
		rename `var'`t' `var'_full_`t'
	}
}

g id=1
reshape long b_full_ se_full_ t_full_ p_full_, i(id) j(t) string

g t1=substr(t, 1, 1)
g t2=substr(t, 2, 4)

rename *M *_M
rename *F *_F

foreach var in b se t p{
	replace `var'_M=. if t1=="F"
	replace `var'_F=. if t1=="M"
}

expand 5 if t2=="0911"
bysort t1 t2: egen rank=rank(id), unique

replace t2="0000" if rank==2
replace t2="0001" if rank==3
replace t2="9998" if rank==4
replace t2="9999" if rank==5

sort t1 t2

replace b_full=. if t2=="9999"
replace b_full=. if t2=="9998"
replace b_full=. if t2=="0000"
replace b_full=. if t2=="0001"

replace b_M=. if t2=="0000" // t1=="M" & 
replace b_M=. if t2=="9999" // t1=="M" & 
replace b_F=. if t2=="0000" // t1=="M" & 
replace b_F=. if t2=="9999" // t1=="M" & 

// Get bounds

rename *full_ *full

foreach case in full M F{
	gen ub10_`case'= b_`case'+invttail(df_full,0.05)*se_`case'
	gen lb10_`case'= b_`case'+invttail(df_full,0.95)*se_`case'
	gen ub5_`case'= b_`case'+invttail(df_full,0.025)*se_`case'
	gen lb5_`case'= b_`case'+invttail(df_full,0.975)*se_`case'
}

// Create time

g time=.
replace time=0 if t1=="M" & t2=="0000"
replace time=0.5 if t1=="M" & t2=="0001"
replace time=1 if t1=="M" & t2=="0911"
replace time=3 if t1=="M" & t2=="0608"
replace time=5 if t1=="M" & t2=="0305"
replace time=7 if t1=="M" & t2=="0002"
replace time=7.5 if t1=="M" & t2=="9998"
replace time=7.75 if t1=="M" & t2=="9999"
replace time=8.25 if t1=="F" & t2=="0000"
replace time=8.5 if t1=="F" & t2=="0001"
replace time=9 if t1=="F" & t2=="0002"
replace time=11 if t1=="F" & t2=="0305"
replace time=13 if t1=="F" & t2=="0608"
replace time=15 if t1=="F" & t2=="0911"
replace time=15.5 if t1=="F" & t2=="9998"
replace time=16 if t1=="F" & t2=="9999"

// Make graph

cap g yline=0
replace y=. if time>0.25 & time<15.75

sort time

twoway ///	
	(rarea ub10_M lb10_M time, fcolor(black) fintensity(2) lcolor(black*.4) lwidth(vthin)) ///
	(line b_M time, lcolor(black*.4)) /// 	
	(rarea ub10_F lb10_F time, fcolor(black) fintensity(2) lcolor(black*.4) lwidth(vthin)) ///	
	(line b_F time, lcolor(black*.4)) /// 
	(line yline time, lcolor(gs12) lpattern(dash)) ///	
	(rcap ub10_full lb10_full time, color(black) lwidth(medthin)) ///
	(scatter b_full time, mcolor(black) msize(medlarge)), /// 
	legend(off) graphregion(color(white)) ///
	xlabel(1 `" "Jan 1, 2009"   "Feb 28, 2009" "' 3 `" "Mar 1, 2009"   "May 31, 2009" "' 5 `" "Jun 1, 2009"   "Aug 31, 2009" "' 7 `" "Sep 1, 2009"   "Nov 29, 2009" "' ///
	9 `" "Nov 30, 2009"   "Feb 28, 2010" "' 11 `" "Mar 1, 2010"   "May 31, 2010" "' 13 `" "Jun 1, 2010"   "Aug 31, 2010" "' 15 `" "Sep 1, 2010"   "Nov 29, 2010" "', labsize(vsmall)) xscale(r(0 16)) ///
	ylabel(-0.05 "-0.05" 0 0.05 "0.05" 0.1 "0.10" 0.15 "0.15" 0.2 "0.20", labsize(small) angle(0)) /// -.05 "-0.05" 0.05 "0.05" 0.15 "0.15" 
	ytitle("Foreign perpetrator coefficient (90% C.I.)") xtitle("Before referendum | After referendum   ", margin(medsmall)) ///
	text(.004 .8 "0.004", place(w) color(black) size(small)) ///
	text(.031 2.8 "0.031", place(w) color(black) size(small)) ///
	text(.038 4.8 "0.038", place(w) color(black) size(small)) ///
	text(.125 6.8 "0.125", place(w) color(black) size(small)) ///
	text(-.003 8.8 "-0.003", place(w) color(black) size(small)) ///
	text(.036 10.8 "0.036", place(w) color(black) size(small)) ///
	text(.023 12.8 "0.023", place(w) color(black) size(small)) ///
	text(.019 14.8 "0.019", place(w) color(black) size(small)) ///
	xline(8, lpattern(dash) lcolor(gs13))
graph export "$Results/$program.pdf", as(pdf) replace				

cap log close
